package com.huluobo.a2209monthdemo

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2209monthdemo.ui.theme._2209MonthDemoTheme
import com.huluobo.a2209monthdemo.view.MainPage
import com.huluobo.a2209monthdemo.view.WelcomePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE
            ), 102
        )
        setContent {
            _2209MonthDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = RoutePath.WELCOME
                    val mmkv = MMKV.defaultMMKV()
                    val navHostController = rememberNavController()

                    val isIn = mmkv.decodeBool(Const.PARAM_IS_IN, false)
                    if (isIn) {
                        startPage = RoutePath.MAIN
                    } else {

                        lifecycleScope.launch {
                            async { delay(5000) }.await()
                            navHostController.navigate(RoutePath.MAIN)
                            mmkv.encode(Const.PARAM_IS_IN, true)
                        }
                    }

                    NavHost(navController = navHostController, startDestination = startPage, builder = {
                        composable(RoutePath.WELCOME) { WelcomePage() }
                        composable(RoutePath.MAIN) { MainPage() }
                    })

                }
            }
        }
    }
}